﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Wormhole.UI.Application;
using SG1 = Wormhole.Engine.CardLibrary.SG1;

namespace Wormhole.Test.UI.Application
{
	/// <summary>
	/// Interaction logic for Villian.xaml
	/// </summary>
	public partial class Villian : Window
	{
		#region Declarations
		protected bool connected = false;

		/// <summary>
		/// The dialog that will be used to display card details.
		/// </summary>	
		protected CardDetails detailsDialog;

		#region Game
		/// <summary>
		/// The Game Engine object.
		/// </summary>
		protected Engine.Game game { get; set; }
		#endregion

		#region Hero Player
		/// <summary>
		/// The hero "AI".
		/// </summary>
		protected HeroPlayer heroPlayer;
		#endregion

		#endregion

		#region Constructor
		/// <summary>
		/// Initialize a new instance of the class.
		/// </summary>
		public Villian()
		{
			InitializeComponent();
		}
		#endregion

		#region Event Handlers

		#region On Closing
		/// <summary>
		/// On Closing
		/// </summary>
		protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
		{
			this.game.Disconnect();
			this.heroPlayer.Disconnect();

			base.OnClosing(e);
			this.detailsDialog.Close();
		}
		#endregion

		#region On Content Rendered
		/// <summary>
		/// On Content Rendered
		/// </summary>
		/// <param name="e"></param>
		protected override void OnContentRendered(EventArgs e)
		{
			this.InitializeGame();
		}
		#endregion

		#region Open Game Received
		/// <summary>
		/// Handle an open game that was received.
		/// </summary>
		/// <param name="game"></param>
		void game_OpenGameReceived(Wormhole.Engine.Communication.Game game)
		{
			this.connected = true;
			this.game.JoinGame(game);
		}
		#endregion

		#region Game Started
		/// <summary>
		/// Handle the game started event.
		/// </summary>
		void game_GameStarted()
		{
			this.game.Start();
		}
		#endregion

		#endregion

		#region Initialize Player Deck
		/// <summary>
		/// Initize the card deck for a player.
		/// </summary>
		/// <param name="player">The player to initialize the deck for.</param>
		private void InitializePlayerDeck(Engine.Player.Player player)
		{
			// team
			player.Deck.Add(new SG1.Cards.Hero.Character.SG1C105(player));
			player.Deck.Add(new SG1.Cards.Hero.Character.SG1C88(player));
			player.Deck.Add(new SG1.Cards.Hero.Character.SG1C95(player));
			player.Deck.Add(new SG1.Cards.Hero.Character.SG1S93(player));

			// support characters
			player.Deck.Add(new SG1.Cards.Hero.Character.SG1C66(player));
			player.Deck.Add(new SG1.Cards.Hero.Character.SG1C66(player));
			player.Deck.Add(new SG1.Cards.Hero.Character.SG1C66(player));
			player.Deck.Add(new SG1.Cards.Hero.Character.SG1C65(player));
			player.Deck.Add(new SG1.Cards.Hero.Character.SG1C65(player));
			player.Deck.Add(new SG1.Cards.Hero.Character.SG1C65(player));

			// events
			player.Deck.Add(new SG1.Cards.Hero.Event.SG1C116(player));
			player.Deck.Add(new SG1.Cards.Hero.Event.SG1C116(player));
			player.Deck.Add(new SG1.Cards.Hero.Event.SG1C131(player));
			player.Deck.Add(new SG1.Cards.Hero.Event.SG1C131(player));
			player.Deck.Add(new SG1.Cards.Hero.Event.SG1C131(player));
			player.Deck.Add(new SG1.Cards.Hero.Event.SG1C132(player));
			player.Deck.Add(new SG1.Cards.Hero.Event.SG1C132(player));
			player.Deck.Add(new SG1.Cards.Hero.Event.SG1C132(player));

			// gear
			player.Deck.Add(new SG1.Cards.Hero.Gear.SG1C142(player));
			player.Deck.Add(new SG1.Cards.Hero.Gear.SG1C142(player));
			player.Deck.Add(new SG1.Cards.Hero.Gear.SG1C143(player));
			player.Deck.Add(new SG1.Cards.Hero.Gear.SG1C143(player));
			player.Deck.Add(new SG1.Cards.Hero.Gear.SG1C148(player));
			player.Deck.Add(new SG1.Cards.Hero.Gear.SG1C148(player));

			// missions
			player.Deck.Add(new SG1.Cards.Hero.Mission.SG1S160(player));
			player.Deck.Add(new SG1.Cards.Hero.Mission.SG1S162(player));
			player.Deck.Add(new SG1.Cards.Hero.Mission.SG1S166(player));
			player.Deck.Add(new SG1.Cards.Hero.Mission.SG1S171(player));
			player.Deck.Add(new SG1.Cards.Hero.Mission.SG1S185(player));
			player.Deck.Add(new SG1.Cards.Hero.Mission.SG1S174(player));
			player.Deck.Add(new SG1.Cards.Hero.Mission.SG1S179(player));
			player.Deck.Add(new SG1.Cards.Hero.Mission.SG1S194(player));
			player.Deck.Add(new SG1.Cards.Hero.Mission.SG1S196(player));
			player.Deck.Add(new SG1.Cards.Hero.Mission.SG1S199(player));
			player.Deck.Add(new SG1.Cards.Hero.Mission.SG1S200(player));
			player.Deck.Add(new SG1.Cards.Hero.Mission.SG1S201(player));

			// adversary
			player.Deck.Add(new SG1.Cards.Villain.Adversary.SG1S1(player));
			player.Deck.Add(new SG1.Cards.Villain.Adversary.SG1S1(player));
			player.Deck.Add(new SG1.Cards.Villain.Adversary.SG1C26(player));
			player.Deck.Add(new SG1.Cards.Villain.Adversary.SG1C26(player));

			// obstacle
			player.Deck.Add(new SG1.Cards.Villain.Obstacle.SG1C216(player));
			player.Deck.Add(new SG1.Cards.Villain.Obstacle.SG1C216(player));

			player.Deck.Add(new SG1.Cards.Villain.Obstacle.SG1C231(player));
			player.Deck.Add(new SG1.Cards.Villain.Obstacle.SG1C231(player));
			player.Deck.Add(new SG1.Cards.Villain.Obstacle.SG1C231(player));

			player.Deck.Add(new SG1.Cards.Villain.Obstacle.SG1C239(player));
			player.Deck.Add(new SG1.Cards.Villain.Obstacle.SG1C239(player));

			player.Deck.Add(new SG1.Cards.Villain.Obstacle.SG1C252(player));
			player.Deck.Add(new SG1.Cards.Villain.Obstacle.SG1C252(player));

			player.Deck.Add(new SG1.Cards.Villain.Obstacle.SG1C258(player));
			player.Deck.Add(new SG1.Cards.Villain.Obstacle.SG1C258(player));

			player.Deck.Add(new SG1.Cards.Villain.Obstacle.SG1C276(player));
			player.Deck.Add(new SG1.Cards.Villain.Obstacle.SG1C276(player));
			player.Deck.Add(new SG1.Cards.Villain.Obstacle.SG1C276(player));

			player.Deck.Add(new SG1.Cards.Villain.Obstacle.SG1C278(player));
			player.Deck.Add(new SG1.Cards.Villain.Obstacle.SG1C278(player));
			player.Deck.Add(new SG1.Cards.Villain.Obstacle.SG1C278(player));

			player.Deck.Add(new SG1.Cards.Villain.Obstacle.SG1C281(player));
			player.Deck.Add(new SG1.Cards.Villain.Obstacle.SG1C281(player));
			player.Deck.Add(new SG1.Cards.Villain.Obstacle.SG1C281(player));
		}
		#endregion

		#region Initialize Game
		private void InitializeGame()
		{
			// create the details dialog
			this.detailsDialog = new CardDetails();
			this.detailsDialog.Owner = this.Owner;

			// create the game
			this.game = new Wormhole.Engine.Game();
			this.InitializePlayerDeck(this.game.Player1);
			this.InitializePlayerDeck(this.game.Player2);

			// configure the villian area
			this.VillianArea.DetailsDialog = this.detailsDialog;
			this.VillianArea.Game = this.game;

			// create the hero player
			this.heroPlayer = new HeroPlayer();

			// connect to the server
			this.game.Connect("localhost", 6667, "Rodney_McKay_");
			this.game.OpenGameReceived += new Wormhole.Engine.Communication.OpenGameReceivedDelegate(game_OpenGameReceived);
			this.game.GameStarted += new Wormhole.Engine.Game.GameStartedDelegate(game_GameStarted);

			//System.Threading.Thread.Sleep(5000);
			//this.game.RequestOpenGameList();
		}
		#endregion

	}
}
